package com.fjwt.gz.service.wx;

import com.fjwt.gz.core.config.WxConfig;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class WxConfigServiceImpl implements IWxConfigService {

    @Autowired
    private WxConfig wxConfig;

    private static Config config;

    private static NotificationConfig notificationConfig;

    @Override
    public Config getConfig() {

        if(config != null){
            return config;
        }

        initConfig();
        return config;
    }

    private void initConfig(){
        config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(wxConfig.getMerchantId())
                        .privateKeyFromPath(wxConfig.getPrivateKeyFromPath())
                        .merchantSerialNumber(wxConfig.getMerchantSerialNumber())
                        .apiV3Key(wxConfig.getApiV3Key())
                        .build();
    }


    @Override
    public NotificationConfig getNotificationConfig()  {

        if(notificationConfig != null){
            return notificationConfig;
        }

        initNotifyConfig();
        return notificationConfig;
    }

    private void initNotifyConfig(){
        notificationConfig = new RSAAutoCertificateConfig.Builder()
                .merchantId(wxConfig.getMerchantId())
                .privateKeyFromPath(wxConfig.getPrivateKeyFromPath())
                .merchantSerialNumber(wxConfig.getMerchantSerialNumber())
                .apiV3Key(wxConfig.getApiV3Key())
                .build();
    }
}
